package com.itheima.demo7test;

import java.util.List;

public class PeopleThread extends  Thread{
    private List< Integer> redPacket;
    public PeopleThread(List<Integer> redPacket, String name) {
        super(name);
        this.redPacket = redPacket;
    }
    public PeopleThread(){}
    //重写run方法
    @Override
    public void run() {
        //谁抢到了红包
        String name = Thread.currentThread().getName();
        while (true) {
            synchronized (redPacket) {
                if (redPacket.size()==0)
                {
                    break;
                }
                //随机索引红包
                int index = (int)(Math.random()*redPacket.size());
                //将红包减去 返回这个红包的值
                Integer r = redPacket.remove(index);
                System.out.println(name+"抢到红包"+r);

                if (redPacket.size()==0)
                {
                    System.out.println("红包抢完了");
                    break;
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
